package com.pet.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.behavior.mapper.ApBehaviorEntryMapper;
import com.pet.behavior.service.ApBehaviorEntryService;
import com.pet.model.behavior.pojos.ApBehaviorEntry;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP行为实体表
 * </p>
 */
@Service
@Transactional
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements ApBehaviorEntryService {

    @Override
    public ApBehaviorEntry findByUserIdOrEquipmentId(Integer userId, Integer equipmentId) {
        QueryWrapper<ApBehaviorEntry> queryWrapper = new QueryWrapper<>();
        if(userId!=null) {
            queryWrapper.eq("entry_id", userId);
            queryWrapper.eq("type", 1);
        }
        if(userId==null&&equipmentId!=null){
            queryWrapper.eq("entry_id",equipmentId);
            queryWrapper.eq("type",0);
        }
        ApBehaviorEntry behaviorEntry = getOne(queryWrapper);
        return behaviorEntry;
    }

    @Override
    public boolean save(Integer userId, Integer equipmentId) {
        ApBehaviorEntry apBehaviorEntry = new ApBehaviorEntry();
        apBehaviorEntry.setCreatedTime(new Date());
        if(userId!=null) {
            apBehaviorEntry.setType(1);
            apBehaviorEntry.setEntryId(userId);
        }
        if(userId==null&&equipmentId!=null){
            apBehaviorEntry.setType(0);
            apBehaviorEntry.setEntryId(equipmentId);
        }
        return save(apBehaviorEntry);
    }
}
